package com.lonwin.inspectionmanagement.app.utils;

import android.graphics.Color;
import android.graphics.Typeface;

import com.lh.LhEarthSurfaceView;
import com.lh.Location.LhLocation;
import com.lh.features.LhCoordinate;
import com.lh.features.renderable.LhPointPlacemark;
import com.lh.features.renderable.LhSurfacePolyline;
import com.lh.layers.LhLayer;
import com.lh.layers.LhLayerList;
import com.lh.layers.LhTiledImageLayer;
import com.lh.layers.imageLayers.BingMapAerialLayer;
import com.lh.layers.imageLayers.GaoDeMapLayer;
import com.lh.layers.imageLayers.GaoDeRoadLayer;
import com.lh.layers.imageLayers.GoogleMapLayer;
import com.lh.layers.imageLayers.TiandituRoadsLayer;
import com.lh.util.LhCoordUtil;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.GpsUpdataInfoBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HistoryRecordingBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.QuestListBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SearLocusBean;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class LhEarthUtils {

    /**
     * 设置路网隐藏还是显示
     * @param
     * @param lhEarthSurfaceView
     */
    public static void setHide(LhEarthSurfaceView lhEarthSurfaceView)
    {
                for(LhLayer layer:lhEarthSurfaceView.getLayers())
                {
                    if (layer.getName().equals("天地图")){
                        layer.setEnabled(!layer.isEnabled());
//                        lhEarthSurfaceView.getLayers().remove(layer);
                        lhEarthSurfaceView.redraw();

                        break;
                    }
                }
    }
    /**
     * 添加图层
     * @param lhEarthSurfaceView
     */
    public static  void addLayers(LhEarthSurfaceView lhEarthSurfaceView, ConfigBean configBean){
//        lhEarthSurfaceView.getLayers().add(new GaoDeMapLayer());//高德地图坐标系
        lhEarthSurfaceView.getLayers().add(new BingMapAerialLayer());//wgs84坐标系
//        lhEarthSurfaceView.getLayers().add(new GoogleMapLayer());//wgs84坐标系
        TiandituRoadsLayer  tiandituRoadsLayer = new TiandituRoadsLayer();
        tiandituRoadsLayer.setEnabled(true);
        tiandituRoadsLayer.setName("天地图");
        lhEarthSurfaceView.getLayers().add(tiandituRoadsLayer);
        //设置俯仰角
        lhEarthSurfaceView.setTilt(1);
        //设置左右旋转角
        lhEarthSurfaceView.setRotateAngle(0);
        lhEarthSurfaceView.setEyeDistance(2000);
        if (configBean!=null&&"532900000000000".equals(configBean.getAdcd()))
        {
            //大理州的加上河湖图层
            LhLayer ly=new LhTiledImageLayer("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Layer layerType=\"TiledImageLayer\" version=\"1\">	<DisplayName>dlRivers</DisplayName>	<DatasetName>dlRivers</DatasetName>	<DataCacheName>dlRivers</DataCacheName>	<Service serviceName=\"OGC:WMS\" version=\"1.3.0\">		<LayerNames>dlRivers</LayerNames>		<GetMapURL>http://120.55.185.135:8080/geoserver/DL/wms?service=WMS		</GetMapURL>		<GetCapabilitiesURL>http://120.55.185.135:8080/geoserver/DL/ows?SERVICE=WMS&amp;		</GetCapabilitiesURL>	</Service>	<FormatSuffix>.png</FormatSuffix>	<NumLevels count=\"19\" numEmpty=\"0\" />	<Sector>		<SouthWest>			<LatLon latitude=\"24.664255104162578\" longitude=\"98.86319237813336\"				units=\"degrees\" />		</SouthWest>		<NorthEast>			<LatLon latitude=\"26.75167320005819\" longitude=\"101.16379508157783\"				units=\"degrees\" />		</NorthEast>	</Sector>	<TileOrigin>		<LatLon latitude=\"-90.0\" longitude=\"-180.0\" units=\"degrees\" />	</TileOrigin>	<TileSize>		<Dimension height=\"512\" width=\"512\" />	</TileSize>	<LevelZeroTileDelta>		<LatLon latitude=\"36.0\" longitude=\"36.0\" units=\"degrees\" />	</LevelZeroTileDelta>	<ImageFormat>image/png</ImageFormat>	" +
                    "<UseTransparentTextures>true</UseTransparentTextures></Layer>","dl");
            lhEarthSurfaceView.getLayers().add(ly);
        }
        lhEarthSurfaceView.setLhEarthIsEdit(true);
        lhEarthSurfaceView.setShowCrossHairs(false);
    }

    /**
     * 绘制问题点
     * @param lhEarthSurfaceView
     * @param dataBeans
     */
    public static void  drawQuestPoint(LhEarthSurfaceView lhEarthSurfaceView,List<QuestListBean.DataBean> dataBeans)
    {
        //设置视野高度
//        lhEarthSurfaceView.setEyeDistance(800);
        lhEarthSurfaceView.setEyeDistance(2000);
        if (dataBeans==null||dataBeans.isEmpty())
        {
            return;
        }
        LhLayerList lhLayers=   lhEarthSurfaceView.getLayers();
        boolean isHaveLocuris=false;//是否有巡河轨迹
        if (lhLayers!=null&&!lhLayers.isEmpty())
        {
            for (LhLayer layer:lhLayers)
            {
                if (layer.getName().equals("巡河轨迹"))
                {
                    isHaveLocuris=true;
               for (QuestListBean.DataBean dataBean:dataBeans)
               {
                   //创建点要素
                   if (dataBean.getY()>10&&dataBean.getX()>10)
                   {
                       LhPointPlacemark start=new LhPointPlacemark(LhCoordinate.fromDegrees(dataBean.getY(),dataBean.getX()));
                       start.setLabelText((dataBean.getQusetTypeTempRel()!=null&&!dataBean.getQusetTypeTempRel().isEmpty())?dataBean.getQusetTypeTempRel().get(0).getQuestTypenm():"");
                       start.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
                       start.setImagePath("image/quest_location.png");
                       start.setScale(1.5d);
                       start.setLabelFontSize(40f);
                       start.setLabelColor(Color.argb(255, 20,20,20));//("#ff00ff00");
                       start.setImageInCenter(true);
                       layer.addFeature(start);
                   }
               }
                }
            }
            if (!isHaveLocuris)
            {
                LhLayer mlayer=new LhLayer();
                mlayer.setName("问题记录点");
                lhEarthSurfaceView.getLayers().add(mlayer);
                for (QuestListBean.DataBean dataBean:dataBeans)
                {
                    if (dataBean.getY()>10&&dataBean.getX()>10)
                    {
                        //创建点要素
                        LhPointPlacemark start=new LhPointPlacemark(LhCoordinate.fromDegrees(dataBean.getY(),dataBean.getX()));
                        start.setLabelText("");
                        start.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
                        start.setImagePath("image/quest_location.png");
                        start.setScale(1.5d);
                        start.setLabelFontSize(40f);
                        start.setLabelColor(Color.argb(255, 0,255,0));//("#ff00ff00");
                        start.setImageInCenter(true);
                        mlayer.addFeature(start);
                    }
                }
            }
        }
        lhEarthSurfaceView.redraw();
    }
    /**
     * 绘制巡河轨迹,新版
     * @param lhEarthSurfaceView
     * @param gpsPointsBeans
     */
    public static void drawCruiseLocusNew(LhEarthSurfaceView lhEarthSurfaceView, List<SearLocusBean.DataBean.GpsPointsBean> gpsPointsBeans) {
        if (gpsPointsBeans==null||gpsPointsBeans.isEmpty())
        {
            return;
        }
        LhLayerList lhLayers=   lhEarthSurfaceView.getLayers();
        //设置中心点
        lhEarthSurfaceView.setLookAtLhCoordinate(LhCoordinate.fromDegrees((gpsPointsBeans.get(0).getLatitude()+gpsPointsBeans.get(gpsPointsBeans.size()-1).getLatitude())/2,(gpsPointsBeans.get(0).getLongitude()+gpsPointsBeans.get(gpsPointsBeans.size()-1).getLongitude())/2));
        //设置左右旋转角
        //设置视野高度
//        lhEarthSurfaceView.setEyeDistance(1000);
        lhEarthSurfaceView.setEyeDistance(2000);
        lhEarthSurfaceView.redraw();
        LhLayer mlayer=new LhLayer();
        mlayer.setName("巡河轨迹");
        lhEarthSurfaceView.getLayers().add(mlayer);
        //创建点要素
        LhPointPlacemark start=new LhPointPlacemark(LhCoordinate.fromDegrees(gpsPointsBeans.get(0).getLatitude(),gpsPointsBeans.get(0).getLongitude()));
        start.setLabelText("");
        start.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        start.setImagePath("image/qd_icon.png");
        start.setScale(1.5d);
        start.setLabelFontSize(40f);
        start.setLabelColor(Color.argb(255, 0,255,0));//("#ff00ff00");
        start.setImageInCenter(true);
        mlayer.addFeature(start);
        if (gpsPointsBeans.size()<2)
        {
            //只有一个点
            lhEarthSurfaceView.redraw();
            return;
        }
        //大于两个点
        LhPointPlacemark end=new LhPointPlacemark(LhCoordinate.fromDegrees(gpsPointsBeans.get(gpsPointsBeans.size()-1).getLatitude(),gpsPointsBeans.get(gpsPointsBeans.size()-1).getLongitude()));
        end.setLabelText("");
        end.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        end.setImagePath("image/zd_icon.png");
        end.setScale(1.5d);
        end.setLabelFontSize(40f);
        end.setLabelColor(Color.argb(255, 255,0,0));//("#ff0000ff");
        end.setImageInCenter(true);
        mlayer.addFeature(end);

        //画线
        List<LhCoordinate> coordList=new ArrayList<>();
        for (SearLocusBean.DataBean.GpsPointsBean gpsPointsBean:gpsPointsBeans)
        {
            if (gpsPointsBean.getLatitude()>10&&gpsPointsBean.getLongitude()>10)
            { coordList.add(LhCoordinate.fromDegrees(gpsPointsBean.getLatitude(),gpsPointsBean.getLongitude(),0.0));

            }
        }
        LhSurfacePolyline lhSurfacePolyline=new LhSurfacePolyline(coordList);
        lhSurfacePolyline.setLineColor(Color.argb(255, 130,128,252));//("#ffffff00");
        lhSurfacePolyline.setLineWidth(10d);
        mlayer.addFeature(lhSurfacePolyline);
        lhEarthSurfaceView.redraw();

    }

    /**
     * 绘制巡河轨迹
     * @param lhEarthSurfaceView
     * @param resultBeans
     */
    public static void drawCruiseLocus(LhEarthSurfaceView lhEarthSurfaceView, List<HistoryRecordingBean.ResultBean> resultBeans) {
        if (resultBeans==null||resultBeans.isEmpty())
        {
            return;
        }
        LhLayerList lhLayers=   lhEarthSurfaceView.getLayers();
        //设置中心点
        lhEarthSurfaceView.setLookAtLhCoordinate(LhCoordinate.fromDegrees((resultBeans.get(0).getLatitude()+resultBeans.get(resultBeans.size()-1).getLatitude())/2,(resultBeans.get(0).getLongitude()+resultBeans.get(resultBeans.size()-1).getLongitude())/2));
        //设置左右旋转角
        //设置视野高度
//        lhEarthSurfaceView.setEyeDistance(1000);
        lhEarthSurfaceView.setEyeDistance(2000);
        lhEarthSurfaceView.redraw();
        LhLayer mlayer=new LhLayer();
            mlayer.setName("巡河轨迹");
            lhEarthSurfaceView.getLayers().add(mlayer);
        //创建点要素
        LhPointPlacemark start=new LhPointPlacemark(LhCoordinate.fromDegrees(resultBeans.get(0).getLatitude(),resultBeans.get(0).getLongitude()));
        start.setLabelText("");
        start.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        start.setImagePath("image/qd_icon.png");
        start.setScale(1.5d);
        start.setLabelFontSize(40f);
        start.setLabelColor(Color.argb(255, 0,255,0));//("#ff00ff00");
        start.setImageInCenter(true);
        mlayer.addFeature(start);
        if (resultBeans.size()<2)
        {
            //只有一个点
            lhEarthSurfaceView.redraw();
            return;
        }
        //大于两个点
        LhPointPlacemark end=new LhPointPlacemark(LhCoordinate.fromDegrees(resultBeans.get(resultBeans.size()-1).getLatitude(),resultBeans.get(resultBeans.size()-1).getLongitude()));
        end.setLabelText("");
        end.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        end.setImagePath("image/zd_icon.png");
        end.setScale(1.5d);
        end.setLabelFontSize(40f);
        end.setLabelColor(Color.argb(255, 255,0,0));//("#ff0000ff");
        end.setImageInCenter(true);
        mlayer.addFeature(end);

        //画线
        List<LhCoordinate> coordList=new ArrayList<>();
        for (HistoryRecordingBean.ResultBean resultBean:resultBeans)
        {
            if (resultBean.getLatitude()>10&&resultBean.getLongitude()>10)
            {
                coordList.add(LhCoordinate.fromDegrees(resultBean.getLatitude(),resultBean.getLongitude(),0.0));
            }
        }
        LhSurfacePolyline lhSurfacePolyline=new LhSurfacePolyline(coordList);
        lhSurfacePolyline.setLineColor(Color.argb(255, 130,128,252));//("#ffffff00");
        lhSurfacePolyline.setLineWidth(10d);
        mlayer.addFeature(lhSurfacePolyline);
        lhEarthSurfaceView.redraw();
    }

    /**
     * 绘制历史轨迹
     * @param lhEarthSurfaceView
     * @param gpsPoints
     */
    public static  void drawHistoryLocus(LhEarthSurfaceView lhEarthSurfaceView, List<SearLocusBean.DataBean.GpsPointsBean> gpsPoints)
    {
        if (gpsPoints==null||gpsPoints.isEmpty())
        {
            return;
        }
        LhLayer mlayer=new LhLayer();
        mlayer.setName("历史巡河轨迹");
        lhEarthSurfaceView.getLayers().add(mlayer);
        //画线
        List<LhCoordinate> coordList=new ArrayList<>();
        for (SearLocusBean.DataBean.GpsPointsBean gpsPointsBean:gpsPoints)
        {
            if (gpsPointsBean.getLatitude()>10&&gpsPointsBean.getLongitude()>10)
            {
                coordList.add(LhCoordinate.fromDegrees(gpsPointsBean.getLatitude(),gpsPointsBean.getLongitude(),0.0));

            }
        }
        LhSurfacePolyline lhSurfacePolyline=new LhSurfacePolyline(coordList);
        lhSurfacePolyline.setLineColor(Color.argb(255, 130,128,252));//("#ffffff00");
        lhSurfacePolyline.setLineWidth(10d);
        mlayer.addFeature(lhSurfacePolyline);
        lhEarthSurfaceView.redraw();
    }
    /**
     * 画轨迹
     * @param lhEarthSurfaceView
     * @param gpsUpdataInfoBeans
     */
    public static void drawLocus(LhEarthSurfaceView lhEarthSurfaceView, List<GpsUpdataInfoBean> gpsUpdataInfoBeans) {
    if (gpsUpdataInfoBeans==null||gpsUpdataInfoBeans.isEmpty())
    {
        return;
    }
        LhLayerList lhLayers=   lhEarthSurfaceView.getLayers();
        LhLayer mlayer=null;
        for (LhLayer lhLayer:lhLayers)
        {
            if (lhLayer.getName().equals("定位"))
            {
                mlayer=lhLayer;
                mlayer.removeAllFeatures();
                break;
            }
        }
        if (mlayer==null)
        {
            mlayer=new LhLayer();
            mlayer.setName("定位");
            lhEarthSurfaceView.getLayers().add(mlayer);
        }

        //创建点要素
        LhPointPlacemark start=new LhPointPlacemark(LhCoordinate.fromDegrees(gpsUpdataInfoBeans.get(0).getLatitude(),gpsUpdataInfoBeans.get(0).getLongitude()));
        start.setLabelText("");
        start.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        start.setImagePath("image/qd_icon.png");
        start.setScale(1.5d);
        start.setLabelFontSize(40f);
        start.setLabelColor(Color.argb(255, 0,255,0));//("#ff00ff00");
        start.setImageInCenter(true);
        mlayer.addFeature(start);
    if (gpsUpdataInfoBeans.size()<2)
    {
        //只有一个点
        lhEarthSurfaceView.redraw();
      return;
     }
     //大于两个点
        LhPointPlacemark end=new LhPointPlacemark(LhCoordinate.fromDegrees(gpsUpdataInfoBeans.get(gpsUpdataInfoBeans.size()-1).getLatitude(),gpsUpdataInfoBeans.get(gpsUpdataInfoBeans.size()-1).getLongitude()));
        end.setLabelText("");
        end.setLabelFontFamily(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
        end.setImagePath("image/zd_icon.png");
        end.setScale(1.5d);
        end.setLabelFontSize(40f);
        end.setLabelColor(Color.argb(255, 255,0,0));//("#ff0000ff");
        end.setImageInCenter(true);
        mlayer.addFeature(end);

        //画线
        List<LhCoordinate> coordList=new ArrayList<>();
        for (GpsUpdataInfoBean gpsUpdataInfoBean:gpsUpdataInfoBeans)
        {
            if (gpsUpdataInfoBean.getLatitude()>10&&gpsUpdataInfoBean.getLongitude()>10) {
                coordList.add(LhCoordinate.fromDegrees(gpsUpdataInfoBean.getLatitude(), gpsUpdataInfoBean.getLongitude(), 0.0));
            }
        }
        LhSurfacePolyline lhSurfacePolyline=new LhSurfacePolyline(coordList);
        lhSurfacePolyline.setLineColor(Color.argb(255, 130,128,252));//("#ffffff00");
        lhSurfacePolyline.setLineWidth(10d);
        mlayer.addFeature(lhSurfacePolyline);
        lhEarthSurfaceView.redraw();

    }

    /**
     * 定位当前位置
     * @param lhEarthSurfaceView
     * @param coord
     */
    public static void locatPosition(LhEarthSurfaceView lhEarthSurfaceView, LhCoordinate coord)
    {
        if (coord.getLatitude()<10||coord.getLongitude()<10)
        {
            return;
        }
        //设置俯仰角
        lhEarthSurfaceView.setTilt(1);
        //设置左右旋转角
        lhEarthSurfaceView.setRotateAngle(0);
//        lhEarthSurfaceView.setEyeDistance(800);
        lhEarthSurfaceView.setEyeDistance(2000);
        LhLayerList lhLayers=   lhEarthSurfaceView.getLayers();

        //设置中心点
        lhEarthSurfaceView.setLookAtLhCoordinate(coord);
		//设置左右旋转角
		//设置视野高度

        lhEarthSurfaceView.redraw();
            LhPointPlacemark ppmk=new LhPointPlacemark(coord);
            ppmk.setImagePath("image/location.png");
            ppmk.setLabelFontSize(15);
            ppmk.setScale(1.5d);
            ppmk.setLabelText("");
        LhLayer mlayer=null;
        for (LhLayer lhLayer:lhLayers)
        {
            if (lhLayer.getName().equals("定位"))
            {
                mlayer=lhLayer;
                mlayer.removeAllFeatures();
                break;
            }
        }
        if (mlayer==null)
        {
            mlayer=new LhLayer();
            mlayer.setName("定位");
            lhEarthSurfaceView.getLayers().add(mlayer);
        }
        mlayer.addFeature(ppmk);

    }



    public static String convertToDMS(double d)
    {
        try
        {
            String str = "" + d;
            int p = str.indexOf(".");
            int dt = Integer.parseInt(str.substring(0, p));
            d = d - dt;
            double M = d * 60;
            int mt = (int) M;
            M = (M - mt) * 60;
            if (Math.abs(M - 60) < 0.001)
            {
                M = 0;
                mt = mt + 1;
            }
            if (mt == 60)
            {
                dt = dt + 1;
                mt = 0;
            }
            DecimalFormat df = new DecimalFormat("0.00");
            return ""+dt+"°"+mt+"′"+df.format(M)+"″";
        } catch(Exception e)  {
            return  e.getMessage();
        }
    }
}
